﻿using System;
using System.Text.RegularExpressions;

namespace RegexpMatching.Matchers
{
  /// <summary>
  /// Represents class with static methods for matching phones
  /// </summary>
  public class PhoneMatcher
  {

    // ((?=\+\s*\d)|(?!\+)) - tests if input starts with "+" and digits after this or starts not from "+"
    // ((\s*\d\s*)(-?\s*\d\s*){0,9}) - tests if input contains only numbers, whitespaces and "-" (between two numbers must be only one (or zero) "-")
    // (\+?(-?\s*\d\s*){0,4})((?'Open'\()(\s*\d\s*)(-?\s*\d\s*){1,3}(?'Close-Open'\))(?(Open)(?!))(?=[\s-]*\d)
    //          (\+?(-?\s*\d\s*){0,4}) - tests if input starts with ("+") and 0 - 4 numbers with whitespaces and "-". 
    //          ((?'Open'\()(\s*\d\s*)(-?\s*\d\s*){1,3}(?'Close-Open'\))(?(Open)(?!)) - tests if where one or zero groups of numbers between "(" and ")"
    //          (?=[\s-]*\d) - tests if we have digits after "(...)"
    // (\s*-?\s*\d\s*)(-?\s*\d\s*){4,9})) - tests if where is 5-10 digits after "(...)"(if exists)

    /// <summary>
    /// Regexp for index matching
    /// </summary>
    private const string PhoneRegexp = @"^\s*((?=\+\s*\d)|(?!\+))(((\s*\d\s*)(-?\s*\d\s*){0,9})|((\+?(-?\s*\d\s*){0,4})((?'Open'\()(\s*\d\s*)(-?\s*\d\s*){1,3}(?'Close-Open'\))(?(Open)(?!))(?=[\s-]*\d))?(\s*-?\s*\d\s*)(-?\s*\d\s*){4,9}))$";

    /// <summary>
    /// Default constructor (hided)
    /// </summary>
    private PhoneMatcher()
    {
    }

    /// <summary>
    /// Tests if string matches phone
    /// </summary>
    /// <param name="input">string to be tested</param>
    /// <returns>true if string represents phone, false otherwise</returns>
    public static Boolean IsMatch(string input)
    {
      return Regex.IsMatch(input, PhoneRegexp);
    }
  }
}
